package com.grandbroad.eim.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.easy.west.westclient.api.base.bean.VodBean;
import com.easy.west.westclient.api.hotvod.bean.VodOrLiveBean;
import com.grandbroad.eim.R;
import com.grandbroad.eim.entry.LiveRoomBean;
import com.grandbroad.eim.ui.activities.LiveActivity;
import com.grandbroad.eim.ui.activities.VideoActivity;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 推荐item的适配器
 */
public class MyHotAdpter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<VodOrLiveBean> vodOrLiveBeanList = new ArrayList<>();

    public void setVodOrLiveBeanList(List<VodOrLiveBean> vodOrLiveBeanList) {
        this.vodOrLiveBeanList = vodOrLiveBeanList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.hot_item, viewGroup, false);
        return new MyHotViewholder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof MyHotViewholder) {
            //判断直播或非直播
            //1 LIVE 2 REC
            MyHotViewholder hotviewholder = (MyHotViewholder) viewHolder;
            VodOrLiveBean vodOrLiveBean = vodOrLiveBeanList.get(i);
            if (vodOrLiveBean.getVodOrLive()==1)
            {
                hotviewholder.tvHotTag.setText("LIVE");
                hotviewholder.tvHotTag.setBackgroundResource(R.drawable.live_tag_bg);
                String imgurl = vodOrLiveBean.getThumbnail();
                x.image().bind(hotviewholder.imgHotImg,imgurl);
                String title = vodOrLiveBean.getName();
                hotviewholder.tvTitle.setText(title);
                String username = vodOrLiveBean.getUser().getName();
                hotviewholder.tvUserName.setText(username);
                //跳直播
                hotviewholder.itemView.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v) {
                        //copy直播实体
                        LiveRoomBean liveRoomBean = new LiveRoomBean();
                        LiveRoomBean.UserBean userBean = new LiveRoomBean.UserBean();
                        liveRoomBean.setChatRoomid(vodOrLiveBean.getChatRoomid());
                        liveRoomBean.setThumbnail(vodOrLiveBean.getThumbnail());
                        liveRoomBean.setIslive(vodOrLiveBean.isIslive());
                        liveRoomBean.setDescript(vodOrLiveBean.getDescript());
                        liveRoomBean.setCreated_at(vodOrLiveBean.getName());
                        liveRoomBean.setId(vodOrLiveBean.getId());
                        liveRoomBean.setHls(vodOrLiveBean.getHls());
                        liveRoomBean.setRtmp(vodOrLiveBean.getRtmp());
                        liveRoomBean.setRtsp(vodOrLiveBean.getRtsp());
                        liveRoomBean.setName(vodOrLiveBean.getName());
                        liveRoomBean.setStick(vodOrLiveBean.getStick());
                        userBean.setProvince(vodOrLiveBean.getUser().getProvince());
                        userBean.setPhone(vodOrLiveBean.getUser().getPhone());
                        userBean.setVerfied(vodOrLiveBean.getUser().isVerfied());
                        userBean.setId(vodOrLiveBean.getUser().getId());
                        userBean.setScreen_name(vodOrLiveBean.getUser().getScreen_name());
                        userBean.setName(vodOrLiveBean.getUser().getName());
                        userBean.setAvatar(vodOrLiveBean.getUser().getAvatar());
                        userBean.setCreated_at(vodOrLiveBean.getUser().getCreated_at());
                        userBean.setCity(vodOrLiveBean.getUser().getCity());
                        userBean.setAddress(vodOrLiveBean.getUser().getAddress());
                        liveRoomBean.setUser(userBean);
                        Bundle b = new Bundle();
                        b.putSerializable("bean",liveRoomBean);
                        Intent intent = new Intent(hotviewholder.itemView.getContext(),LiveActivity.class);
                        intent.putExtras(b);
                        hotviewholder.itemView.getContext().startActivity(intent);
                    }
                });
            }
            else
            {
                hotviewholder.tvHotTag.setText("REC");
                hotviewholder.tvHotTag.setBackgroundResource(R.drawable.rec_tag_bg);
                String imgurl = (String) vodOrLiveBean.getCover_img();
                x.image().bind(hotviewholder.imgHotImg,imgurl);
                String title = vodOrLiveBean.getName();
                hotviewholder.tvTitle.setText(title);
                String username = vodOrLiveBean.getUser().getName();
                hotviewholder.tvUserName.setText(username);
                //跳点播
                hotviewholder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        VodBean vodBean = new VodBean();
                        VodBean.UserBean userBean = new VodBean.UserBean();
                        vodBean.setName(vodOrLiveBean.getName());
                        vodBean.setUrl(vodOrLiveBean.getUrl());
                        vodBean.setStick(vodOrLiveBean.getStick());
                        vodBean.setState(vodOrLiveBean.getState());
                        vodBean.setCreated_at(vodOrLiveBean.getCreated_at());
                        vodBean.setCount(vodOrLiveBean.getCount());
                        vodBean.setId(vodOrLiveBean.getId());
                        vodBean.setDescrib(vodOrLiveBean.getDescrib());
                        vodBean.setCover_img(vodOrLiveBean.getCover_img());
                        userBean.setId(vodOrLiveBean.getUser().getId());
                        userBean.setVerfied(vodOrLiveBean.getUser().isVerfied());
                        userBean.setName(vodOrLiveBean.getUser().getName());
                        userBean.setProvince(vodOrLiveBean.getUser().getProvince());
                        userBean.setScreen_name(vodOrLiveBean.getUser().getScreen_name());
                        userBean.setCreated_at(vodOrLiveBean.getUser().getCreated_at());
                        userBean.setCity(vodOrLiveBean.getUser().getCity());
                        userBean.setAvatar(vodOrLiveBean.getUser().getAvatar());
                        userBean.setAddress(vodOrLiveBean.getUser().getAddress());
                        vodBean.setUser(userBean);
                        Bundle vb = new Bundle();
                        vb.putSerializable("bean",vodBean);
                        Intent intent = new Intent(hotviewholder.itemView.getContext(),VideoActivity.class);
                        intent.putExtras(vb);
                        hotviewholder.itemView.getContext().startActivity(intent);
                    }
                });
            }

        }
    }

    @Override
    public int getItemCount() {
        return vodOrLiveBeanList.size();
    }

    class MyHotViewholder extends RecyclerView.ViewHolder {

        @BindView(R.id.img_hot_img)
        ImageView imgHotImg;
        @BindView(R.id.tv_hot_tag)
        TextView tvHotTag;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_user_name)
        TextView tvUserName;

        public MyHotViewholder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
